package com.example.home.fragment.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.fragment.activity.NewsDetailsActivity;
import com.example.home.fragment.bean.NewsListDataBean;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class BingnewlistApter extends RecyclerView.Adapter<BingnewlistApter.MyHolder> {
    private Context context;
    private List<NewsListDataBean.RowsBean> list;

    public BingnewlistApter(Context context, List<NewsListDataBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public BingnewlistApter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_newdislist, null);
        return new BingnewlistApter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BingnewlistApter.MyHolder holder, int position) {
        Glide.with(context).load(ip + list.get(position).getCover()).into(holder.newsdislist_img);
        holder.newsdislist_img.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.newsdislist_title.setText(list.get(position).getTitle());
        holder.newsdislist_link.setText("观看人数:" + list.get(position).getReadNum());
        holder.newsdislist_dianzeng.setText("点赞人数:" + list.get(position).getLikeNum());
        holder.re_newsdislist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NewsDetailsActivity.class);
                intent.putExtra("NewsFram_id", String.valueOf(list.get(position).getId()));
                context.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView newsdislist_img;
        TextView newsdislist_title;
        TextView newsdislist_link;
        TextView newsdislist_dianzeng;
        RelativeLayout re_newsdislist;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            newsdislist_img = itemView.findViewById(R.id.newsdislist_img);
            newsdislist_title = itemView.findViewById(R.id.newsdislist_title);
            newsdislist_link = itemView.findViewById(R.id.newsdislist_link);
            newsdislist_dianzeng = itemView.findViewById(R.id.newsdislist_dianzeng);
            re_newsdislist = itemView.findViewById(R.id.re_newsdislist);
        }
    }
}
